home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / SmallTalk / WriteStream.st < prev    next >
Text File  |  1995-08-25  |  4KB  |  171 lines

  1. "======================================================================
  2. |
  3. |   WriteStream Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbb          1 Jan 91      Added print: and store: so that printing and storing
  36. |                         can be cascaded.
  37. |
  38. | sbyrne     20 May 90      Fixed semantics of write streams so that they return
  39. |                         only the characters that have been written to them.
  40. |
  41. | sbyrne     19 Sep 89      Changed to use real method categories.
  42. |
  43. | sbyrne     25 Apr 89      created.
  44. |
  45. "
  46.  
  47. PositionableStream subclass: #WriteStream
  48.            instanceVariableNames: 'maxSize'
  49.            classVariableNames: ''
  50.            poolDictionaries: ''
  51.            category: nil
  52. !
  53.  
  54. WriteStream comment: 
  55. 'I am the class of writeable streams.  I only allow write operations to
  56. my instances; reading is strictly forbidden.' !
  57.  
  58. !WriteStream class methodsFor: 'instance creation'!
  59.  
  60. on: aCollection
  61.     ^(self new initCollection: aCollection) access: 2
  62. !
  63.  
  64. with: aCollection
  65.     | stream |
  66.     stream _ self on: aCollection.
  67.     stream moveToEnd.
  68.     ^stream
  69. !
  70.  
  71. with: aCollection from: firstIndex to: lastIndex
  72.     | stream |
  73.     stream _ self on: aCollection fromFirstIndex to: lastIndex.
  74.     stream moveToEnd.
  75.     ^stream
  76. !!
  77.  
  78.  
  79.  
  80. !WriteStream methodsFor: 'accessing-writing'!
  81.  
  82. nextPut: anObject
  83.     "(access bitAnd: 2) = 0
  84.        ifTrue: [ ^self shouldNotImplement ]."
  85.     ptr > maxSize ifTrue: [ self growCollection ].
  86.     collection at: ptr put: anObject.
  87.     ptr > endPtr ifTrue: [ endPtr _ ptr ].
  88.     ptr _ ptr + 1.
  89.     ^anObject
  90. !!
  91.  
  92.  
  93.  
  94. !WriteStream methodsFor: 'positioning'!
  95.  
  96. setToEnd
  97.     ptr _ endPtr + 1
  98. !!
  99.  
  100.  
  101.  
  102. !WriteStream methodsFor: 'character writing'!
  103.  
  104. cr
  105.     self nextPut: Character cr
  106. !
  107.  
  108. nl
  109.     self nextPut: Character nl
  110. !
  111.  
  112. crTab
  113.     self cr.
  114.     self tab
  115. !
  116.  
  117. nlTab
  118.     self nl.
  119.     self tab
  120. !
  121.  
  122. space
  123.     self nextPut: Character space
  124. !
  125.  
  126. tab
  127.     self nextPut: Character tab
  128. !!
  129.  
  130.  
  131.  
  132. !WriteStream methodsFor: 'stream printing'!
  133.  
  134. print: anObject
  135.     anObject printOn: self
  136. !
  137.  
  138. store: anObject
  139.     anObject storeOn: self
  140. !!
  141.  
  142.  
  143.  
  144. !WriteStream methodsFor: 'private methods'!
  145.  
  146. initCollection: aCollection
  147.     collection _ aCollection.
  148.     ptr _ 1.
  149.     endPtr _ 0.
  150.     maxSize _ aCollection size
  151. !
  152.  
  153. moveToEnd
  154.     endPtr _ collection size.
  155.     self setToEnd
  156. !
  157.  
  158. workingSize
  159.     "Return the actual number of valid elements in the stream"
  160.     ^ptr max: endPtr
  161. !
  162.  
  163. growCollection
  164.     collection grow.
  165.     maxSize _ collection size
  166.  
  167. !!
  168.  
  169.